package cn.com.cifi.mars.util;

import cn.com.cifi.mars.bean.bo.StartAndEndTimeBO;
import cn.com.cifi.mars.bean.vo.signPlan.SignPlanProgressVo;
import cn.com.cifi.mars.bean.vo.signPlan.signOverall.MonthSignVo;
import cn.com.cifi.mars.bean.vo.signPlan.signOverall.OverallSignPlanVo;
import cn.com.cifi.mars.bean.vo.signPlan.signOverall.YearSignVo;
import cn.com.cifi.mars.constant.WhetherEnum;
import cn.com.cifi.mars.entity.SignPlanProgress;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * 类名称：SignPlanProgressTemplate {
 * 类描述：签约计划进度表模板
 * 创建人：jyx
 * 创建时间：2020年07月029日 上午16:25:00
 * @version 1.0.0
 */

@Slf4j
public class SignPlanProgressTemplate {
    private static final String TEMPLATE_PATH = "/template/sign_plan_progress_template.xlsx";

    private XSSFWorkbook workbook;

    private XSSFSheet sheet;

    //默认sheet索引
    private int defaultSheetIndex = 0;
    //数据开始行
    private int dataBeginRow = 1;
    //当前行标
    private int currentDataRowCursor = dataBeginRow;
    //单元格样式
    private XSSFCellStyle cellStyle;
    //文字样式
    private XSSFCellStyle cellStyle1;
    //数值样式
    private XSSFCellStyle cellStyle2;

    private  BigDecimal wan = new BigDecimal(10000);


    public SignPlanProgressTemplate(String name) {

        try {
            InputStream tempInputStream = SignPlanProgressTemplate.class.getResourceAsStream(TEMPLATE_PATH);
            if (tempInputStream != null) {
                workbook = new XSSFWorkbook(tempInputStream);
                sheet = workbook.getSheetAt(defaultSheetIndex);
                setSheetDate(name);
                XSSFRow row = sheet.getRow(dataBeginRow);
                cellStyle1 = row.getCell(0).getCellStyle();
                cellStyle2 = row.getCell(12).getCellStyle();

            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }


    public void setSheetDate(String name) {
        workbook.setSheetName(defaultSheetIndex, workbook.getSheetName(defaultSheetIndex).replace("签约计划进度表", name));
    }

    /**
     * 批量插入行
     * @param list 报表数据
     */
    public synchronized void appendRows(List<SignPlanProgress> list) {
        for (SignPlanProgress progress : list) {
            this.appendRow(progress);
        }
    }

    /**
     * 插入行
     *
     * @param
     */
    public synchronized void appendRow(SignPlanProgress progress) {
        XSSFRow row = sheet.getRow(currentDataRowCursor);
        if (row == null) {
            row = sheet.createRow(currentDataRowCursor);
        }
        int index = 0;
        //项目信息
        cellStyle = cellStyle1;
        this.appendCell(row, index++,progress.getBusinessDivisionName());
        this.appendCell(row, index++,progress.getCityCompanyName());
        this.appendCell(row, index++,progress.getProjectCode());
        this.appendCell(row, index++,progress.getProjectName());
        this.appendCell(row, index++,progress.getStageCode());
        this.appendCell(row, index++,progress.getStageName());
        this.appendCell(row, index++,progress.getSignVersionName());
        this.appendCell(row, index++,progress.getVersionStateName());
        this.appendCell(row, index++,progress.getCreateByName());
        this.appendCell(row, index++,null == progress.getCreateTime() ? "" : LocalDateTimeUtils.formatTime(progress.getCreateTime(),"yyyy/MM/dd HH:mm"));
        this.appendCell(row, index++,progress.getVersionCreateName());
        this.appendCell(row, index++,null == progress.getSuccessApprovalTime() ? "" : LocalDateTimeUtils.formatTime(progress.getSuccessApprovalTime(),"yyyy/MM/dd HH:mm"));
        cellStyle = cellStyle2;
        this.appendCell(row, index++,null == progress.getYearSignTargetAmount() ? 0 : BigDecimalUtils.divBigDecimal(progress.getYearSignTargetAmount(),wan,0).longValue());
        this.appendCell(row, index++,null == progress.getDifferenceAmount() ? 0 : BigDecimalUtils.divBigDecimal(progress.getDifferenceAmount(),wan,0).longValue());
        cellStyle = cellStyle1;
        this.appendCell(row, index++,progress.getSoureSupplyVersionName());
        currentDataRowCursor++;

    }

    /**
     * 插入列
     *
     * @param row
     * @param index
     * @param value
     */
    public synchronized void appendCell(XSSFRow row, int index, String value) {
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value == null ? "" : value);
    }

    /**
     * 插入列
     *
     * @param row
     * @param index
     * @param value
     */
    public synchronized void appendCell(XSSFRow row, int index, Long value) {
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value);
    }

    /**
     * 导出
     * @param outputStream
     * @throws IOException
     */
    public void write(OutputStream outputStream) throws IOException {
        workbook.write(outputStream);
    }
}
